#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QTimer>
#include<QDebug>
#include<QLabel>
#include"playscene.h"
#include<QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("选择关卡");
    //创建菜单栏
    QMenuBar *bar = this->menuBar();
    this->setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //创建按钮菜单项
    QAction *quitAction = startMenu->addAction("退出");
    //点击退出，退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮的音效
    QSound* backSound = new QSound(":/res/BackButtonSound.wav",this);

    //创建返回按钮
    MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");

    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());

    connect(closeBtn,&MyPushButton::clicked,[=](){
        //qDebug()<<"bcak";
        //告诉主场景,我返回了
        backSound->play();
        emit this->chooseSceneBack();
    });
    //选择关卡的音效
    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav",this);

    //创建选择关卡的按钮
    for(int i = 0;i<20;i++){
        MyPushButton *menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4*70,130+i/4*70);
        //
        connect(menuBtn,&MyPushButton::clicked,[=](){
            chooseSound->play();
            QString str = QString("您选择的是第%1关").arg(i+1);
            qDebug()<<str;

            play = new PlayScene(i+1);
            //设置游戏场景的初始位置
            play->setGeometry(this->geometry());
            this->hide();//选关场景隐藏
            play->show();//显示游戏场景
            connect(play,&PlayScene::playSceneBack,[=](){
                backSound->play();
                this->setGeometry(play->geometry());
                play->hide();
                this->show();
                delete play;
                play = NULL;
            });
        });

        QLabel *label = new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->move(25+i%4*70,130+i/4*70);

        //设置label上的文字对齐方式
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //label挡住了btn，设置让鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);


    }




}


void ChooseLevelScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);

}



